<?php
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
/**
*@description: member model using for login, logout ...
*/
class Member extends AppModel {

    /**
    * Use database config
    *
    * @var string
    */
    //public $useDbConfig = 'admin';
	public $useTable = 'staffs';
	/**
 	* Display field
 	*
 	* @var string
 	*/
    public $displayField = 'name';

    /**
    * Validation rules
    *
    * @var array
    */
    public $validate = array(
        'loginid' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'password' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'name' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'group_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'del_flg' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

	/**
	 *
	 * @return Ambigous <boolean, multitype:>
	 */
    public function changePassValidate() {
    	$changeValidate = array(
    			'currentPass' => array(
    					'required' => array(
    							'rule' => array('notEmpty'),
    							'message' => 'an_currentPass_is_required'),
    					'checkCurrentPass' => array(
    							'rule' => 'checkCurrentPass',
    							'message' => 'check_current_pass'),
    					'minLength' => array(
    							'rule' => array('minLength', 8),
    							'message' => 'please_enter_at_least_8_characters'
    					)
    			),
    			'newPass' => array(
    					'required' => array(
    							'rule' => array('notEmpty'),
    							'message' => 'an_newPass_is_required'),
    					'minLength' => array(
    							'rule' => array('minLength', 8),
    							'message' => 'please_enter_at_least_8_characters'
    					),
    					'compareCurrentPass' => array(
    							'rule' => 'compareCurrentPass',
    							'message' => 'Password the same as the current is specified'
    					),
    					'validateLang' => array(
    							'rule' => 'validateLang',
    							'message' => 'pass_language'
    					),
    			),
    			'confirmPass' => array(
    					'required' => array(
    							'rule' => array('notEmpty'),
    							'message' => 'an_confirmPass_is_required'),
    					'minLength' => array(
    							'rule' => array('minLength', 8),
    							'message' => 'please_enter_at_least_8_characters'
    					),
    					'compareCurrentPass' => array(
    							'rule' => 'compareCurrentPass',
    							'message' => 'Password the same as the current is specified'
    					),
    					'validateLang' => array(
    							'rule' => 'validateLang',
    							'message' => 'pass_language'
    					),

    			)
    	);

    	$this->validate = $changeValidate;
    	return $this->validates();
    }

    /**
     * Check current password admin
     * @return number
     */
    public function checkCurrentPass(){
    	$password = Security::hash($this->data['Member']['loginid'] . $this->data['Member']['currentPass'] . Configure :: read('Security.salt'), 'sha256', true);
    	$currentPass = $this->find('first', array('conditions' => array(
    			'password' => $password,
    			'del_flg' => 0
    	)));

    	return count($currentPass);
    }

    /**
     * Compare current pass and new pass
     * @return boolean
     */
    public function compareCurrentPass() {
    	if ($this->data['Member']['newPass'] === $this->data['Member']['currentPass'] || $this->data['Member']['confirmPass'] === $this->data['Member']['currentPass']) {
    		return FALSE;
    	}

    	return TRUE;
    }

    /**
     * Check length password
     * @return boolean
     */
    public function validateLang(){
    	if (strlen($this->data['Member']['newPass']) != strlen(utf8_decode($this->data['Member']['newPass']))) {
    		return FALSE;
    	} else {
    		return TRUE;
    	}
    }

    /**
     * Update password
     * @param string $pass
     * @param int $id
     * @return unknown
     */
    public function updatePass($pass, $id) {
    	try {
    		$data = $this->query(
    				"UPDATE staffs SET password =" . "'" . $pass . "'" .
    				"WHERE staffs.id = " . $id);
    	} catch (Exception $e) {
    		$this->_exception(__FUNCTION__, $e, CConst::API_ERROR);
    	}

    	return $data;
    }
}
